package GUI;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import javax.swing.JScrollPane;

import Client.BusinessImplements;


import Controler.Update_Book_Details_controler;

import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import javax.swing.border.BevelBorder;
import javax.swing.UIManager;
import javax.swing.border.CompoundBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.LineBorder;
/**
* @author Lucie
 * Class name  Update_Deatails_Book
 * extend  Abstract_Jpanel
 * purpose: update the details of the item that the user choose
 */
public class Update_Deatails_Book extends Abstract_Jpanel {
	private JTextField textFieldname;
	private JTextField textFieldid;
	private JTextField textFieldcopis;
	private JTextField textFieldedition;
	private JTextField textFieldwanted;
	private JTextField textFieldlocation;
	private JTextArea textArea;
	private JTable table_1;
	private JTable table;
	private JButton btnBack;
	private JPanel panel;
	private JPanel panel_1;
	private JLabel lblUpdateBookDeatails;
	private JComboBox comboBox_author_name;
	private JComboBox comboBox_book_subject;
	private ArrayList<String> all_authors_foritem;
	private ArrayList<String> all_subject_foritem;
	private String[] item_authors_name;
	private String[] item_subject_name;
	
	
	private ArrayList<String> all_authors;
	private ArrayList<String> all_subject;
	private String[] all_authors_name;
	private String[] all_subject_name;
	private String[] all_authors_id;
	private String[] all_subject_id;
	private Update_Book_Details_controler up_book_deatails;
	private ArrayList<String> item_alldeatails;

	/**
	 * Create the panel.
	 */
	public Update_Deatails_Book() {
		setLayout(null);
		
		panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(20, 38, 979, 646);
		add(panel);
		panel.setLayout(null);
		
		comboBox_author_name = new JComboBox();
	
		
		JLabel lblAddNewAutho = new JLabel("add/delete new author:");
		lblAddNewAutho.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblAddNewAutho.setBounds(406, 511, 150, 14);
		panel.add(lblAddNewAutho);
		
		
		comboBox_author_name.setBounds(566, 510, 109, 20);
		panel.add(comboBox_author_name);
		/*
		 * btnaddauthor ;call the controller of this panel to add an author to the item
		 */
		JButton btnaddauthor = new JButton("add");
		btnaddauthor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Update_Book_Details_controler up_book_deatails1=new Update_Book_Details_controler();
		 		if (!comboBox_author_name.getSelectedItem().toString().isEmpty())
		 		{
		 		
						for(int i=0;i< all_authors_name.length;i++)
						{
							if(all_authors_name[i].equals(comboBox_author_name.getSelectedItem()))
							{	
								if(!up_book_deatails1.addAuthorToItem(item_alldeatails.get(0), all_authors_id[i]))
								{
									 next_screen_faild(up_book_deatails1.get_msg());	
								}
								else
								{
									on_screen_load(item_alldeatails);
									next_screen_success();
								}
						   		break;
						   	}
						}
		 		}
		 		
			}
		});
		btnaddauthor.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnaddauthor.setBounds(685, 507, 89, 23);
		panel.add(btnaddauthor);
		/*
		 * btnDeleteauthor= call the controller of this panel to delete an author from the item
		 */
		JButton btnDeleteauthor = new JButton("delete");
		btnDeleteauthor.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnDeleteauthor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Update_Book_Details_controler up_book_deatails1=new Update_Book_Details_controler();
		 		if (!comboBox_author_name.getSelectedItem().toString().isEmpty())
		 		{
		 		
						for(int i=0;i< all_authors_name.length;i++)
						{
							if(all_authors_name[i].equals(comboBox_author_name.getSelectedItem()))
							{	
								if(!up_book_deatails1.deleteAuthorofItem(item_alldeatails.get(0), all_authors_id[i]))
								{
									 next_screen_faild(up_book_deatails1.get_msg());	
								}
								else
								{
									on_screen_load(item_alldeatails);
									next_screen_success();
								}
						   		break;
						   	}
						}
		 		}
			}
		});
		btnDeleteauthor.setBounds(790, 507, 89, 23);
		panel.add(btnDeleteauthor);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new MatteBorder(6, 6, 6, 6, (Color) new Color(240, 240, 240)));
		panel_2.setBackground(new Color(230, 230, 250));
		panel_2.setBounds(10, 317, 877, 154);
		panel.add(panel_2);
		panel_2.setLayout(null);
		
		JLabel lblSubjects = new JLabel("current subjects:");
		lblSubjects.setBounds(10, 11, 123, 20);
		panel_2.add(lblSubjects);
		lblSubjects.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 36, 360, 107);
		panel_2.add(scrollPane);
		
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
					{new Integer(1), null},
					{new Integer(2), null},
					{new Integer(3), null},
					{new Integer(4), null},
					{new Integer(5), null},
					{new Integer(6), null},
					{new Integer(7), null},
				{null, null},
			},
			new String[] {
				"", "subject name"
			}
		));
		
		table.getColumnModel().getColumn(1).setPreferredWidth(72);
		scrollPane.setViewportView(table);
		
		JLabel lblAddNewSubject = new JLabel("add/delete new subject:");
		lblAddNewSubject.setBounds(395, 53, 150, 17);
		panel_2.add(lblAddNewSubject);
		lblAddNewSubject.setFont(new Font("Tahoma", Font.PLAIN, 14));
		comboBox_book_subject = new JComboBox();
		comboBox_book_subject.setBounds(554, 53, 109, 20);
		panel_2.add(comboBox_book_subject);
		/*
		 * btnaddsub=call the controller of this panel to add an subject to the item
		 */
		JButton btnaddsub = new JButton("add");
		btnaddsub.setBounds(670, 50, 89, 23);
		panel_2.add(btnaddsub);
		btnaddsub.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
		 	
		 		if (!comboBox_book_subject.getSelectedItem().toString().isEmpty())
		 		{
		 			Update_Book_Details_controler up_book_deatails1=new Update_Book_Details_controler();
						for(int i=0;i< all_subject_name.length;i++)
						{
							if(all_subject_name[i].equals(comboBox_book_subject.getSelectedItem()))
							{	
								if(!up_book_deatails1.addSubjectToItem(item_alldeatails.get(0), all_subject_id[i]))
								{
									 next_screen_faild(up_book_deatails1.get_msg());	
								}
								else
								{
									on_screen_load(item_alldeatails);
									next_screen_success();
								}
						   		break;
						   	}
						}
				
			 		
		 		}
		 		
			}
		});
		btnaddsub.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JButton btnDeletesub = new JButton("delete");
		btnDeletesub.setBounds(769, 50, 89, 23);
		panel_2.add(btnDeletesub);
		//btnDeletesub=call the controller of this panel to delete a subject from this item
		btnDeletesub.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

		 		if (!comboBox_book_subject.getSelectedItem().toString().isEmpty())
		 		{
		 			Update_Book_Details_controler up_book_deatails1=new Update_Book_Details_controler();
						for(int i=0;i< all_subject_name.length;i++)
						{
							if(all_subject_name[i].equals(comboBox_book_subject.getSelectedItem()))
							{	
								if(!up_book_deatails1.deleteSubjectofItem(item_alldeatails.get(0), all_subject_id[i]))
								{
									 next_screen_faild(up_book_deatails1.get_msg());	
								}
								else
								{
									on_screen_load(item_alldeatails);
									next_screen_success();
								}
						   		break;
						   	}
						}
				
			 		
		 		}
		 		
			
				
				
			}
		});
		btnDeletesub.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblToAddNew = new JLabel("to add/delete new subject you must press add/delete");
		lblToAddNew.setBounds(469, 93, 379, 14);
		panel_2.add(lblToAddNew);
		lblToAddNew.setForeground(Color.RED);
		lblToAddNew.setFont(new Font("Segoe Print", Font.PLAIN, 13));
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new MatteBorder(6, 6, 6, 6, (Color) new Color(227, 227, 227)));
		panel_3.setBackground(new Color(230, 230, 250));
		panel_3.setBounds(10, 482, 877, 146);
		panel.add(panel_3);
		panel_3.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 28, 365, 107);
		panel_3.add(scrollPane_1);
		
		
		table_1 = new JTable();
		table_1.setModel(new DefaultTableModel(
			new Object[][] {
					{new Integer(1), null},
					{new Integer(2), null},
					{new Integer(3), null},
					{new Integer(4), null},
					{new Integer(5), null},
					{new Integer(6), null},
					{new Integer(7), null},
					{null, null},
			},
			new String[] {
				"", "author name"
			}
		));
		table_1.getColumnModel().getColumn(1).setPreferredWidth(72);
		scrollPane_1.setViewportView(table_1);
		
		JLabel lblOutherName = new JLabel("curret author:");
		lblOutherName.setBounds(10, 11, 91, 14);
		panel_3.add(lblOutherName);
		lblOutherName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblToAddNew_1 = new JLabel("to add/delete new author you must pess add/delete");
		lblToAddNew_1.setBounds(467, 83, 400, 14);
		panel_3.add(lblToAddNew_1);
		lblToAddNew_1.setForeground(Color.RED);
		lblToAddNew_1.setFont(new Font("Segoe Print", Font.PLAIN, 13));
		
		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new LineBorder(SystemColor.control, 6));
		panel_4.setBackground(new Color(220, 220, 220));
		panel_4.setBounds(10, 11, 561, 295);
		panel.add(panel_4);
		panel_4.setLayout(null);
		
		textFieldname = new JTextField();
		textFieldname.setBounds(131, 11, 124, 20);
		panel_4.add(textFieldname);
		textFieldname.setColumns(10);
		
		JLabel lblBookName = new JLabel("Book Name:");
		lblBookName.setBounds(10, 12, 75, 14);
		panel_4.add(lblBookName);
		lblBookName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		textFieldid = new JTextField();
		textFieldid.setBounds(131, 39, 124, 20);
		panel_4.add(textFieldid);
		textFieldid.setEnabled(false);
		textFieldid.setColumns(10);
		
		JLabel lblBookNumber = new JLabel("Book Number:");
		lblBookNumber.setBounds(10, 40, 141, 14);
		panel_4.add(lblBookNumber);
		lblBookNumber.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblEdition = new JLabel("edition");
		lblEdition.setBounds(10, 70, 46, 20);
		panel_4.add(lblEdition);
		lblEdition.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		textFieldedition = new JTextField();
		textFieldedition.setBounds(131, 70, 125, 20);
		panel_4.add(textFieldedition);
		textFieldedition.setColumns(10);
		
		textFieldcopis = new JTextField();
		textFieldcopis.setBounds(131, 101, 123, 20);
		panel_4.add(textFieldcopis);
		textFieldcopis.setEnabled(false);
		textFieldcopis.setColumns(10);
		
		JLabel lblNumberOfCopies = new JLabel("Number Of Copies:");
		lblNumberOfCopies.setBounds(10, 101, 123, 14);
		panel_4.add(lblNumberOfCopies);
		lblNumberOfCopies.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		textFieldwanted = new JTextField();
		textFieldwanted.setBounds(131, 132, 124, 20);
		panel_4.add(textFieldwanted);
		textFieldwanted.setColumns(10);
		
		JLabel lblWanted = new JLabel("wanted:");
		lblWanted.setBounds(10, 127, 63, 27);
		panel_4.add(lblWanted);
		lblWanted.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblLocation = new JLabel("location:");
		lblLocation.setBounds(10, 165, 75, 14);
		panel_4.add(lblLocation);
		lblLocation.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblYesOrNo = new JLabel("yes or no only");
		lblYesOrNo.setBounds(276, 132, 94, 19);
		panel_4.add(lblYesOrNo);
		lblYesOrNo.setForeground(Color.RED);
		lblYesOrNo.setFont(new Font("Segoe Print", Font.PLAIN, 12));
		
		textFieldlocation = new JTextField();
		textFieldlocation.setBounds(131, 164, 127, 20);
		panel_4.add(textFieldlocation);
		textFieldlocation.setColumns(10);
		
		 textArea = new JTextArea();
		 textArea.setBounds(131, 195, 325, 86);
		 panel_4.add(textArea);
		 
		 JLabel lblDescription = new JLabel("description:");
		 lblDescription.setBounds(10, 195, 75, 20);
		 panel_4.add(lblDescription);
		 lblDescription.setFont(new Font("Tahoma", Font.PLAIN, 14));
		 
		 panel_1 = new JPanel();
		 panel_1.setBounds(20, 692, 961, 46);
		 add(panel_1);
		  panel_1.setLayout(null);
		 /*
		  * btnBack=goes back to Update_Details_Book_Select_Book panel
		  */
		  btnBack = new JButton("Back");
		  btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		  btnBack.setBounds(10, 0, 103, 34);
		  panel_1.add(btnBack);
		  
		  JButton btnAdd = new JButton("Update");
		  btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 14));
		  btnAdd.setBounds(801, 0, 109, 34);
		  panel_1.add(btnAdd);
		  
		  lblUpdateBookDeatails = new JLabel("Update Book Details");
		  lblUpdateBookDeatails.setForeground(new Color(25, 25, 112));
		  lblUpdateBookDeatails.setFont(new Font("MV Boli", Font.BOLD, 30));
		  lblUpdateBookDeatails.setBounds(88, 0, 399, 40);
		  add(lblUpdateBookDeatails);
		  /*
		   * btnAdd call the controller to update all the changes that the user entered
		   */
		  btnAdd.addActionListener(new ActionListener() {
		  	public void actionPerformed(ActionEvent arg0) {
		  		
		  		Update_Book_Details_controler up_book_deatails1=new Update_Book_Details_controler();
		 	
								if(!up_book_deatails1.updateitem(get_id(), get_name(), get_edition(), get_wanted(), get_description(),get_location()))
								{
									
									 next_screen_faild(up_book_deatails1.get_msg());	
								}
								else
								{ String id =item_alldeatails.get(0);
									item_alldeatails=up_book_deatails1.get_item(id);
									on_screen_load(item_alldeatails);
									next_screen_success();
								}
						   
		 		
		  		
		  		
		  	}
		  });
		 btnBack.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		
		 		Sign_in_panel sip=new Sign_in_panel();
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(sip);
				sip.setSize(getMaximumSize());
				sip.on_screen_load();
		 	}
		 });

	}

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	
	public void on_screen_load() {
		// TODO Auto-generated method stub
		
	}
/**
 *  * this method is the first method we call in this screen
	 * the main purpose of this method is to initialize the comboBox and text fields
	 * and tables
	 * that this panel use, here we call the controller of this panel
	 * to get all author and subjects names and id's and set them at the combobox
	 * and we also get the authors and subjects that relate to the item and set them to the tables
	 * 
 * @param item_deatails
 */
	
	public void on_screen_load(ArrayList<String> item_deatails) {
		
		item_alldeatails=new ArrayList<String>();
		all_authors_foritem=new ArrayList<String>();
		all_subject_foritem=new ArrayList<String>();
		item_alldeatails=item_deatails;
		up_book_deatails=new Update_Book_Details_controler();
		all_authors=up_book_deatails.get_all_authors();
		all_subject=up_book_deatails.get_all_subjects();
		 all_subject_foritem=up_book_deatails.get_sub_of_item(item_alldeatails.get(0));
		 all_authors_foritem=up_book_deatails.get_author_of_item(item_alldeatails.get(0));
		
		add_all_authors_tocommbobox(all_authors);
		add_all_sub_tocommbobox(all_subject);
		settextfields(item_alldeatails.get(0),item_alldeatails.get(1),item_alldeatails.get(2),item_alldeatails.get(3),item_alldeatails.get(4),item_alldeatails.get(5),item_alldeatails.get(6));
		set_table( all_authors_foritem,all_subject_foritem);
		

		
	}


	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	
	

/**
 * 
 * @return
 */
	public String get_id()
	{
		return textFieldid.getText();
	}
	/**
	 * 
	 * @return item name
	 */
	public String get_name()
	{
		return textFieldname.getText();
	}
	/**
	 * 
	 * @return number of copies
	 */
	public String get_copis()
	{
		return textFieldcopis.getText();
	}
	/**
	 * 
	 * @return edition
	 */
	public String get_edition()
	{
		return textFieldedition.getText();
	}
	/**
	 * 
	 * @return "1" if textFieldwanted=YES 
	 * 			"2" if textFieldwanted=NO
	 */
	public String get_wanted()
	{ if (textFieldwanted.getText().equals("YES"))
		return "1";
	if (textFieldwanted.getText().equals("NO"))
				return "0";
		return "";
	}
	/**
	 * 
	 * @return location
	 */
	public String get_location()
	{
		return textFieldlocation.getText();
	}
	/**
	 * 
	 * @return description
	 */
	public String get_description()
	{
		return textArea.getText();
	}
	

	/**
	 * 
	 * @param all_authors
	 * set ll authors in comboBox_author_name
	 */
	public void add_all_authors_tocommbobox(ArrayList<String> all_authors)
	{
		String[] line;
		all_authors_name = new String[all_authors.size()+1];
		all_authors_id = new String[all_authors.size()+1];
		all_authors_name[0] = "";
		all_authors_id[0] =  "";
		for (int i = 1 ; i <= all_authors.size() ; i++)
		{
			line = all_authors.get(i-1).split(",");
			all_authors_id[i] = line[0];
			all_authors_name[i] = line[1];
			
		}
		
		comboBox_author_name.setModel(new DefaultComboBoxModel(all_authors_name));
	
	}
	/**
	 * 
	 * @param all_subject
	 * set all subject in comboBox_book_subject
	 */
	public void add_all_sub_tocommbobox(ArrayList<String> all_subject)
	{
		String[] line;
		all_subject_name = new String[all_subject.size()+1];
		all_subject_id = new String[all_subject.size()+1];
		all_subject_name[0] = "";
		all_subject_id[0] =  "";
		for (int i = 1 ; i <= all_subject.size() ; i++)
		{
			line = all_subject.get(i-1).split(",");
			all_subject_id[i] = line[0];
			all_subject_name[i] = line[1];
			
		}
		
		comboBox_book_subject.setModel(new DefaultComboBoxModel(all_subject_name));
		
	}
	
	/**
	 * 
	 * @param all_authors_foritem
	 * @param all_subject_foritem
	 * set authors and subject that relate to the item in tables
	 */
	public void set_table( ArrayList<String> all_authors_foritem,ArrayList<String> all_subject_foritem)
	{
		String[] colums=new String[]{"", "author name"};
		String[] colums1=new String[]{"", "subject name"};
        Object[][] data=new Object[all_authors_foritem.size()][2];
        Object[][] data1=new Object[all_subject_foritem.size()][2];
        if (!all_authors_foritem.get(0).equals("false"))
        {
	        for (int i = 0; i < all_authors_foritem.size(); i++) {
	        	String hist= new String(Integer.toString(i+1));
	        	data[i][0]=hist;
	        	data[i][1]=all_authors_foritem.get(i);
	        	
			}
	        table_1.setModel(new DefaultTableModel(data,colums));
        }
        
        if (!all_subject_foritem.get(0).equals("false"))
	      {
	        for (int i = 0; i < all_subject_foritem.size(); i++) {
	        	String hist= new String(Integer.toString(i+1));
	        	data1[i][0]=hist;
	        	data1[i][1]=all_subject_foritem.get(i);
	        	
			}
	        table.setModel(new DefaultTableModel(data1,colums1));
        }
	}
	/**
	 * set text fields
	 */
	public void settextfields(String id,String name, String copies,String edition,String wanted,String descri,String location)
	{
			textFieldid.setText(id);
			textFieldname.setText(name);
			textFieldcopis.setText(copies);
			textFieldedition.setText(edition);
			if (wanted.equals("0"))
				textFieldwanted.setText("NO");
			else
				textFieldwanted.setText("YES");
			textFieldlocation.setText(location);
			textArea.setText(descri);
	}
	/**
	 *  this method display showMessageDialog with a success msg
	 */
	public void next_screen_success()
	{
		JOptionPane.showMessageDialog(this,"apply success");
	}
	/**
	 * this method display showMessageDialog with a failed msg
	 * @param msg
	 */
	public void next_screen_faild(String msg)
	{
		JOptionPane.showMessageDialog(this,"apply faild "+msg);
	}
}
